package v1

import (
	"gin-demo/models"
	customValidator "gin-demo/pkg/customValidator"
	"gin-demo/pkg/setting"
	"gin-demo/pkg/util"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

// 获取单个文章
func GetArticle(c *gin.Context) {
	// 获取路由参数的id
	id := com.StrTo(c.Param("id")).MustInt()
	response := util.NewResponseSuccess()
	if id <= 0 {
		response.Code = 422
		response.Msg = "ID必须大于0"
		util.ResponseSuccess(c, response)
		return
	}
	data := models.GetArticle(id)
	if data.ID <= 0 {
		response.Code = 422
		response.Msg = "信息不存在"
		util.ResponseSuccess(c, response)
		return
	}

	response.Data = data
	util.ResponseSuccess(c, response)
}

// 获取文章列表
func GetArticles(c *gin.Context) {
	data := make(map[string]interface{})
	maps := make(map[string]interface{})
	state := com.StrTo(c.DefaultQuery("state", "-1")).MustInt()
	tagId := com.StrTo(c.DefaultQuery("tag_id", "-1")).MustInt()

	response := util.NewResponseSuccess()

	if state != -1 {
		maps["state"] = state
	}
	if tagId != -1 {
		maps["tag_id"] = tagId
	}
	// 分页获取文章列表
	data["lists"] = models.GetArticles(util.GetPage(c), setting.PageSize, maps)
	data["total"] = models.GetArticleTotal(maps)

	response.Data = data
	util.ResponseSuccess(c, response)

}

// 新增文章
func AddArticle(c *gin.Context) {
	var article models.Article
	article.TagId = com.StrTo(c.PostForm("tag_id")).MustInt()
	article.Title = c.PostForm("title")
	article.Desc = c.PostForm("desc")
	article.Content = c.PostForm("content")
	article.CreatedBy = c.PostForm("created_by")
	article.State = com.StrTo(c.PostForm("state")).MustInt()

	response := util.NewResponseSuccess()

	// 数据校验
	validate := customValidator.Validate(article)
	if validate != nil {
		response.Code = 422
		response.Msg = validate.Error()
		util.ResponseSuccess(c, response)
		return
	}

	// 添加文章
	id := models.AddArticle(article)
	response.Data = id
	util.ResponseSuccess(c, response)
}

// 修改文章
func EditArticle(c *gin.Context) {
	var article models.Article
	id := com.StrTo(c.PostForm("id")).MustInt()
	article.TagId = com.StrTo(c.PostForm("tag_id")).MustInt()
	article.Title = c.PostForm("title")
	article.Desc = c.PostForm("desc")
	article.Content = c.PostForm("content")
	article.CreatedBy = c.PostForm("created_by")
	article.ModifiedBy = c.PostForm("modified_by")
	article.State = com.StrTo(c.PostForm("state")).MustInt()

	response := util.NewResponseSuccess()

	// id 字段校验
	idStruct := struct {
		ID int `json:"id" validate:"required,numeric" label:"文章id"`
	}{
		ID: id,
	}
	idVali := customValidator.Validate(idStruct)
	if idVali != nil {
		response.Code = 422
		response.Msg = idVali.Error()
		util.ResponseSuccess(c, response)
		return
	}

	// 数据校验
	validate := customValidator.Validate(article)
	if validate != nil {
		response.Code = 422
		response.Msg = validate.Error()
		util.ResponseSuccess(c, response)
		return
	}

	// 编辑文章
	models.EditArticle(id, article)
	util.ResponseSuccess(c, response)
}

// 删除文章
func DeleteArticle(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	response := util.NewResponseSuccess()
	if id == 0 {
		response.Code = 422
		response.Msg = "ID不能为空"
		util.ResponseSuccess(c, response)
		return
	}
	ret := models.DeleteArticle(id)
	if ret == false {
		util.ResponseSuccess(c, util.NewResponseFailed())
		return
	} else {
		util.ResponseSuccess(c, response)
	}

}
